<template lang="pug">
  .mould
    Card.mould-header
      span 页面模型名称：
      Input(v-model='modelName',search,enter-button="查询",placeholder='请输入页面模型名称',:maxlength="50",@on-search='handleUpdateTableData()')
      Button(v-if="$route.meta['/pagemodel/add']",@click="$router.push({name:'mould_setting',query:{status:'add'}})") 新增
    Card.mould-body
      site-list(ref='mouldSite',:siteLists="sites",@changeSite='handleChangeSite')
      show-table(ref='tab',:total='total',:current='current',:columns="columns",:data='mouldData',@handleChangePage='handleChangePage')
</template>

<script>
import siteList from '@/components/site-list/site-list'
import showTable from '@/components/show-table/show-table'
import { asyncMould } from '../api'

export default {
  name: 'MouldCenter',
  components: { siteList, showTable },
  data() {
    return {
      modelName: '',
      sites: [{ siteName: '全部', siteId: '' }],
      siteIndex: '',
      total: 0,
      current: 1,
      mouldData: [],
      columns: [
        { title: 'ID', width: 60, align: 'center', key: 'modelId' },
        {
          title: '模型名称',
          render: (h, params) => {
            if (this.$route.meta['/pagemodel/query']) {
              const queryParams = {
                status: 'modify',
                modelId: params.row.modelId,
                checkType: params.row.checkType.toString(),
                updatePersonId: params.row.updatePersonId.toString()
              }
              return h('router-link', { attrs: { to: { name: 'mould_setting', query: queryParams } } }, params.row.modelName)
            } else {
              return h('span', params.row.modelName)
            }
          }
        },
        { title: '站点', align: 'center', width: 100, key: 'siteName' },
        { title: '创建时间', align: 'center', width: 80, key: 'createTimes' },
        { title: '创建人', align: 'center', width: 150, key: 'createName' },
        {
          title: '签出状态',
          width: 110,
          align: 'center',
          key: 'checkType',
          render: (h, params) => {
            switch (params.row.checkType) {
              case 0:
                return h('span', { class: { checkin: true } }, '已签入')
              case 1:
                return h('Tooltip', { attrs: { placement: 'top-start', content: `已签出${params.row.updateName}` } }, [
                  h('span', { class: { checkout: true } }, `已签出${params.row.updateName}`)
                ])
            }
          }
        },
        { title: '签出时间', align: 'center', width: 80, key: 'updateTimes' }
      ],
      dynamiColumns: [
        {
          title: '操作',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h(
              'Poptip',
              {
                attrs: { confirm: true, title: '确定要删除这条任务吗?', placement: 'top-end', width: '200' },
                on: { 'on-ok': () => this.handleDeleteModel(params.row.modelId, params.index) }
              },
              [h('Button', { attrs: { icon: 'md-trash', title: '删除' }, style: { color: '#f04134', background: '#f7f7f7' }, class: { 'operate-btn': true } })]
            )
          }
        }
      ]
    }
  },
  mounted() {
    if (this.$route.meta['/pagemodel/delete']) this.columns = [...this.columns, ...this.dynamiColumns]
    this.handleSiteNum()
    this.handleMouldTableData()
  },
  methods: {
    handleSiteNum: async function() {
      try {
        this.sites = [{ siteName: '全部', siteId: '' }]
        this.sites = [...this.sites, ...(await asyncMould.sitenum({ data: { typeId: 1 } })).data.data]
        this.$refs['mouldSite'].showSitePanel = false
        if (this.sites.length - 1 > this.$refs['mouldSite'].showSiteMax) {
          this.$refs['mouldSite'].showSitePanel = true
        }
      } catch (error) {}
    },
    handleChangePage(pageSub) {
      this.handleUpdateTableData(pageSub)
    },
    handleUpdateTableData(current = 1) {
      this.current = current
      this.handleMouldTableData()
    },
    handleMouldTableData: async function() {
      try {
        this.total = 0
        this.mouldData = []
        this.$refs.tab.loading = true
        const res = await asyncMould.list({ data: { pageIndex: this.current, pageSize: this.$refs.tab.pageSize, siteId: this.siteId, modelName: this.modelName } })
        this.total = res.data.data.recordCount
        this.mouldData = res.data.data.result
        this.$refs.tab.loading = false
      } catch (error) {
        this.$refs.tab.loading = false
      }
    },
    handleChangeSite(siteId, index) {
      this.siteIndex = index
      this.siteId = siteId
      this.handleUpdateTableData()
    },
    handleDeleteModel: async function(modelId, index) {
      try {
        await asyncMould.delete({ data: { modelId } })
        this.mouldData.splice(index, 1)
        this.handleUpdateTableData(this.mouldData.length !== 0 ? this.current : --this.current !== 0 ? this.current : (this.current = 1))
        --this.$refs['mouldSite'].siteLists[this.siteIndex].number
        if (this.$refs['mouldSite'].siteLists[this.siteIndex].number === 0) {
          this.$refs['mouldSite'].showSiteId = this.siteId = ''
          this.handleSiteNum()
          this.handleUpdateTableData()
        }
        this.$Message.success('删除成功')
      } catch (error) {}
    }
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>



